package com.topinfo.etl.service;

import com.topinfo.component.base.bean.JsonResponse;
import com.topinfo.component.base.dao.IModelDao;
import com.topinfo.component.base.service.AbstractModelService;
import com.topinfo.etl.bean.EtlDatasourceCfg;
import com.topinfo.etl.dao.EtlDatasourceCfgDao;
import com.topinfo.etl.util.DbUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
* 数据源配置信息
*/
@Service("etlDatasourceCfgService")
public class EtlDatasourceCfgService extends AbstractModelService<EtlDatasourceCfg> {
    @Resource
    private EtlDatasourceCfgDao etlDatasourceCfgDao;

    @Override
    public IModelDao<EtlDatasourceCfg> getModelDao() {
        return this.etlDatasourceCfgDao;
    }

    public JsonResponse connTest(EtlDatasourceCfg model) {
        String driver = DbUtil.getDriverClassName(model.getType());
        String dbUrl = DbUtil.getDbUrl(model.getType(), model.getServerAddr(), model.getPort(), model.getName());
        Connection conn = DbUtil.getDbConn(driver, dbUrl, model.getUser(), model.getPassword());
        if (conn == null) {
            return JsonResponse.getFail();
        }
        return JsonResponse.getSucc();
    }


}
